package com.glwan.fixedassetapp.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KingDeeCommonUtil {

    private static final String TAG = "KingDeeCommonUtil";
    public static Map<String,String> getAssetType() {
        Map<String,String> map= new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "FA_ASSETTYPE");
        data.addProperty("FieldKeys", "FID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getAssetType: 获取资产类别失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getAssetType map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getAssetType: 获取资产类别异常", e);
            return map;
        }
    }

    //计量单位
    public static Map<String,String> getAssetUnit() {
        Map<String,String> map= new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "BD_UNIT");
        data.addProperty("FieldKeys", "FUNITID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getAssetUnit: 获取计量单位失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getAssetUnit map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getAssetUnit: 获取计量单位异常", e);
            return map;
        }
    }
    //资产位置
    public static Map<String, String> getAssetPosition() {
        Map<String,String> map=new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "FA_POSITION");
        data.addProperty("FieldKeys", "FID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getAssetPosition: 获取资产位置失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getAssetPosition map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getAssetPosition: 获取资产位置异常", e);
            return map;
        }
    }

    //资产部门
    public static Map<String, String> getUseDept() {
        Map<String,String> map=new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "BD_Department");
        data.addProperty("FieldKeys", "FDEPTID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getUseDept: 获取资产部门失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getUseDept map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getUseDept: 获取资产部门异常", e);
            return map;
        }
    }

    //费用项目
    public static Map<String, String> getExpense() {
        Map<String,String> map=new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "BD_Expense");
        data.addProperty("FieldKeys", "FEXPID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getUseDept: 获取费用项目失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getExpense map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getUseDept: 获取费用项目异常", e);
            return map;
        }
    }

    //资产状态
    public static Map<String,String> getAssetStatus() {
        Map<String,String> map=new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "FA_STATUS");
        data.addProperty("FieldKeys", "FID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getAssetStatus: 获取资产状态失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getAssetStatus map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getAssetStatus: 获取资产状态异常", e);
            return map;
        }
    }

    //会计政策
    public static Map<String,String> getAcctPolicy() {
        Map<String,String> map=new HashMap<>();
        IdentifyInfo identifyInfo = KingDeeUtil.getIdentifyInfo();
        K3CloudApi api = new K3CloudApi(identifyInfo);
        JsonObject data = new JsonObject();
        data.addProperty("FormId", "BD_ACCTPOLICY");
        data.addProperty("FieldKeys", "FACCTPOLICYID,FNumber,FName");
        try {
            //调用接口
            List<List<Object>> lists = api.executeBillQuery(data.toString());
            for (List<Object> innerList : lists) {
                for (int i = 0; i < innerList.size(); i++) {
                    if (innerList.get(0).toString().contains("IsSuccess=false")) {
                        Log.e(TAG, "getAcctPolicy: 获取会计政策失败: " + innerList.get(0).toString());
                        break;
                    }
                    map.put(innerList.get(1).toString(), innerList.get(2).toString());
                }
            }
            Log.i(TAG, "getAcctPolicy map: " + new Gson().toJson(map));
            return map;
        } catch (Exception e) {
            Log.e(TAG, "getAcctPolicy: 获取会计政策异常", e);
            return map;
        }
    }


}
